import 'package:flutter/material.dart';
import 'package:gzmall_app/common/api.dart';
import 'package:gzmall_app/common/net_util.dart';
import 'package:gzmall_app/common/refresh_listview.dart';
import 'package:gzmall_app/model/deposit.dart';

class TopupRecordPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return TopupRecordPageState();
  }
}

class TopupRecordPageState extends State<TopupRecordPage> {
  List<Deposit> _depositList = [];

  int _curPage = 1;
  RefreshListView _refreshListView;
  bool _showEmpty = false;

  @override
  void initState() {
    super.initState();
    _getDeposits(1);
  }

  @override
  Widget build(BuildContext context) {
    _refreshListView = RefreshListView(
      enablePullUp: true,
      enablePullDown: true,
      onRefresh: _onRefresh,
      child: ListView.builder(
        itemCount: _depositList == null ? 0 : _depositList.length,
        itemBuilder: (context, index) {
          Deposit deposit = _depositList[index];
          return Container(
            color: Colors.white,
            margin: const EdgeInsets.only(bottom: 1.0),
            child: ListTile(
              title: Text(deposit.money),
              subtitle: Text(deposit.createdAt),
              trailing: Text(deposit.state),
            ),
          );
        },
      ),
      showEmpty: _showEmpty,
      onReload: () {
        _getDeposits(1);
      },
    );
    return Scaffold(
      appBar: _createAppBar(),
      body: _refreshListView,
    );
  }

  _createAppBar() {
    return AppBar(
      elevation: 0.0,
      title: Text("充值明细"),
      leading: IconButton(
        icon: Icon(Icons.arrow_back_ios),
        onPressed: () {
          Navigator.of(context).pop();
        },
      ),
      actions: <Widget>[],
    );
  }

  void _onRefresh(bool up) {
    if (up) {
      _getDeposits(1);
    } else {
      _getDeposits(_curPage + 1);
    }
  }

  _getDeposits(int page) {
    bool isGetNew = page == 1;
    Map<String, Object> params = {"page": page.toString()};
    NetUtil.get(
        context,
        Api.deposits,
        (data, message) {
          if (!mounted) return;
          _curPage = page;
          var _dataList = (data as List)
              ?.map((e) => e == null ? null : Deposit.fromJson(e))
              ?.toList();
          setState(() {
            _showEmpty =
                isGetNew && (_dataList == null || _dataList.length <= 0);
            if (isGetNew) {
              _depositList = _dataList;
            } else {
              _depositList.addAll(_dataList);
            }
          });
          if (_refreshListView != null) {
            _refreshListView.refreshCompleted(isGetNew,
                hasMore: _dataList != null && _dataList.length >= Api.pageSize);
          }
        },
        isInterceptError: false,
        params: params,
        errorCallBack: (e) {
          if (!mounted) return;
          setState(() {
            _showEmpty =
                isGetNew && (_depositList == null || _depositList.length <= 0);
          });
          if (_refreshListView != null) {
            _refreshListView.refreshCompleted(isGetNew);
          }
        });
  }
}
